package com.dlwx.parttimetalent.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.dlwx.parttimetalent.R;

/**
 * Created by Administrator on 2017/11/29.
 */

public class OverLayImageView extends View {
    private Paint paint;
    private int w,h;
    public OverLayImageView(Context context) {
        this(context,null);
    }

    public OverLayImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public OverLayImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        paint=new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.w=w;
        this.h=h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(w/2,h/2);
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.icon_rzshenfenb);
        int bw=bitmap.getWidth();
        int bh=bitmap.getHeight();
        float scaleWidth=Math.abs((float)w/bw);
        float scaleHeight=Math.abs((float)h/bh);
        Matrix matrix=new Matrix();
        matrix.postScale(scaleWidth,scaleHeight);
//        LogUtils.d(bw+"-->"+bh);
//        LogUtils.d((w/bw)+"-->"+scaleHeight);
        Bitmap nb=Bitmap.createBitmap(bitmap,0,0,bw,bh,matrix,true);
        Path path=new Path();
        path.addCircle(0,0,w/2 , Path.Direction.CCW);
        canvas.save();
        canvas.clipPath(path);
        canvas.drawBitmap(nb, -w/2, -h/2, paint);
        canvas.restore();

    }
}
